package com.yx.shop.service.impl;

import com.yx.shop.dao.OrderDao;
import com.yx.shop.dao.impl.OrderDaoImpl;
import com.yx.shop.entitly.Item;
import com.yx.shop.entitly.Orders;
import com.yx.shop.service.OrderService;
import com.yx.shop.utils.JDBCUtils;

import java.util.List;

/**
 * @Classname OrderServiceImpl
 * @Description TODO:
 * @Date 2021/05/09 14:00
 * @Created by yx
 */
public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao = new OrderDaoImpl();
    @Override
    public boolean createOrder(Orders orders) {
        boolean flag = false;
        try {
            JDBCUtils.begin();
            if (orders!=null) {
                if (orderDao.createOrder(orders)>0) {
                    flag=true;
                }
            }
            JDBCUtils.commit();
        } catch (Exception e) {
            JDBCUtils.rollback();
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public List<Orders> getOrderListByUid(int uid) {
        List<Orders> ordersList = null;
        try {
            JDBCUtils.begin();
            if (uid>0){
               ordersList = orderDao.getOrderListByUid(uid);
            }
            JDBCUtils.commit();
        } catch (Exception e) {
            JDBCUtils.rollback();
            e.printStackTrace();
        }
        return ordersList;
    }

    @Override
    public Orders getOrderByOid(String oid) {
        //1.根据oid查询订单与订单地址信息
        Orders orders = orderDao.selectOrderByOid(oid);
        //2.根据oid查询对应订单项及商品信息
        List<Item> itemList = orderDao.selectItemListByOid(oid);
        //3.将订单项集合保存至订单对象
        orders.setItemList(itemList);

        return orders;
    }

    @Override
    public boolean updateStateByOid(String oid, Integer state) {
        boolean flag = false;
        try {
            JDBCUtils.begin();
            if (orderDao.updateStateByOid(oid,state)>0) {
                flag=true;
            }
            JDBCUtils.commit();
        } catch (Exception e) {
            JDBCUtils.rollback();
            e.printStackTrace();
        }
        return flag;
    }
}
